package cn.comtom.ewbs.signature.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import cn.comtom.ewbs.signature.common.vo.request.PageRequest;
import cn.comtom.ewbs.signature.common.vo.response.ApiEntityResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiPageResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiResponse;
import cn.comtom.ewbs.signature.entity.SysPermissionRole;
import cn.comtom.ewbs.signature.service.ISysPermissionRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import cn.comtom.ewbs.signature.controller.BaseController;

/**
 * <p>
 * 角色权限中间表 前端控制器
 * </p>
 *
 * @author liuhy
 * @since 2022-11-04 18:07:24
 */
@RestController
@RequestMapping("/auth/sys-permission-role")
@Slf4j
@Validated
@Api(tags = "角色权限中间表管理", description = "角色权限中间表管理")
public class SysPermissionRoleController extends BaseController {

	@Autowired
	private ISysPermissionRoleService sysPermissionRoleService;
	
	/**   
     * 分页查询
     * @param sysPermissionRole 请求参数 
     * @param pageReq   分页参数
     * @return ApiPageResponse 响应结果     
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public ApiPageResponse<SysPermissionRole> page(@ModelAttribute SysPermissionRole sysPermissionRole, @ModelAttribute PageRequest pageReq) {
    	IPage<SysPermissionRole> pager = sysPermissionRoleService.selectPage(sysPermissionRole,pageReq);
        return ApiPageResponse.ok(pager);
    }
	
    /**
     * 根据ID查询
     * @param id 主键
     * @return 响应结果
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    public ApiEntityResponse<SysPermissionRole> getById(@PathVariable(name = "id") String id){
        return ApiEntityResponse.ok(sysPermissionRoleService.getById(id));
    }

    /**
     * 根据ID删除
     * @param id 主键
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public ApiResponse deleteById(@PathVariable(name = "id") String id){
    	if(sysPermissionRoleService.removeById(id)) {
    		return ApiResponse.ok();
    	}else {
    		return ApiResponse.error();
    	}
    }

    /**
     * 保存
     * @param sysPermissionRole 请求数据
     * @return 响应结果
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "保存")
    public ApiResponse save(@Validated @RequestBody SysPermissionRole sysPermissionRole){
    	if(sysPermissionRoleService.saveOrUpdate(sysPermissionRole)) {
    		return ApiResponse.ok();
    	}else {
    		return ApiResponse.error();
    	}
    }
    
    /**
     * 更新
     * @param sysPermissionRole 请求数据
     * @return 响应结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    public ApiResponse update(@Validated @RequestBody SysPermissionRole sysPermissionRole){
    	if(sysPermissionRoleService.saveOrUpdate(sysPermissionRole)) {
    		return ApiResponse.ok();
    	}else {
    		return ApiResponse.error();
    	}
    }

}
